package com.example.newspublish.service.impl;

import com.example.newspublish.mapper.UserMapper;
import com.example.newspublish.pojo.dto.UserDTO;
import com.example.newspublish.pojo.entity.User;
import com.example.newspublish.pojo.form.RegisterForm;
import com.example.newspublish.service.UserService;
import com.example.newspublish.util.JwtTokenManager;
import com.example.newspublish.util.result.Result;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UserMapper userMapper;

    @Autowired
    private AuthenticationManager authenticationManager;

    @Autowired
    private JwtTokenManager jwtTokenManager;

    @Override
    public Result login(String username, String password) {
        System.out.println("登录请求业务层调用1");
            // 验证用户名和密码
//            Authentication authentication = authenticationManager.authenticate(
//                    new UsernamePasswordAuthenticationToken(username, password)
//            );

        System.out.println("登录请求业务层调用2");
        int row = userMapper.login(username,password);

        if(row == 1) {
            System.out.println("登录成功~");
            // 生成Token
            User user = new User();
            user.setUsername(username);
            user.setPassword(password);
            String token = jwtTokenManager.generateToken(user);
            System.out.println("登录返回token:"+token);
            Authentication authentication = new UsernamePasswordAuthenticationToken(username, password, AuthorityUtils.NO_AUTHORITIES);
            // 如果验证通过，将用户信息存入SecurityContext中
            SecurityContextHolder.getContext().setAuthentication(authentication);
            System.out.println("登录认证信息:"+authentication);
            System.out.println("登录请求业务层调用3");
            return Result.success("登录成功~",token);
        }
        else {
            System.out.println("登陆失败，用户名或密码错误~");
            System.out.println("登录请求业务层调用4");
            return Result.success("登陆失败，用户名或密码错误~",null);
        }


    }

    @Override
    public Result register(RegisterForm registerForm) {

        registerForm.setCreateTime(new Timestamp(System.currentTimeMillis()));
        int row = userMapper.register(registerForm);

        if(row == 1) {
            System.out.println("注册成功~");

            return Result.success("注册成功~",null);
        }
        else {
            System.out.println("注册失败，用户名已经存在~");

            return Result.failed(20001,"注册失败，用户名已经存在~");
        }

    }

    /**
     * 修改用户信息
     * @param userDTO
     */
    @Override
    public void updateInfo(UserDTO userDTO) {
        User user = new User();
        BeanUtils.copyProperties(userDTO, user);
        user.setUpdateTime(new Timestamp(System.currentTimeMillis()));

        userMapper.update(user);
    }


}
